package com.example.springstrategydemo.strategy;

import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Component;

import java.util.List;
import java.util.Optional;

/**
 * 使用Spring容器获取所有策略实现类
 */
@Component
@RequiredArgsConstructor
public class GameStrategyFactory {

    private final List<IGameStrategy> gameStrategyList;

    public IGameStrategy getGameStrategy(Game game) {
        Optional<IGameStrategy> optional = gameStrategyList.stream()
                .filter(gameStrategy -> gameStrategy.isSupport(game))
                .findFirst();
        IGameStrategy gameStrategy = optional.orElseThrow(() -> new RuntimeException("找不到游戏！"));
        return gameStrategy;
    }

}
